/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.vo.ErrorMessage;
import com.excesys.exsecs.om.entity.OmRepairModel;
import com.excesys.exsecs.om.mapper.OmRepairModelMapper;
import com.excesys.exsecs.om.service.OmRepairModelService;
import com.excesys.exsecs.om.vo.RepairModelExcelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class OmRepairModelServiceImpl extends ServiceImpl<OmRepairModelMapper, OmRepairModel>
		implements OmRepairModelService {

	private final OmRepairModelMapper omRepairModelMapper;

	@Override
	public IPage<OmRepairModel> getRepairModelPage(Page page, OmRepairModel repairModel) {
		return omRepairModelMapper.getRepairModelPage(page, repairModel);
	}

	@Override
	public R importBamSBuildings(List<RepairModelExcelVo> excelVOList, BindingResult bindingResult) {
		// 通用校验获取失败的数据
		List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
		int sucess = 0;
		int fail = 0;

		// 执行数据插入操作 组装 RoleDto
		for (RepairModelExcelVo excel : excelVOList) {
			// 个性化校验逻辑
			List<OmRepairModel> postList = this.list();
			// 检验区域或者NFC编码是否存在
			Set<String> errorMsg = new HashSet<>();
			boolean existPost = postList.stream()
					.anyMatch(post -> excel.getModelCode().equals(post.getModelCode())
							|| excel.getHeadPhone().equals(post.getHeadPhone())
							|| excel.getRepairPeoplePhone().equals(post.getRepairPeoplePhone()));
			if (existPost) {
				errorMsg.add("设备编码/负责人电话/维修人电话存在重复：" + excel.getModelCode() + "/" + excel.getHeadPhone() + "/"
						+ excel.getRepairPeoplePhone());
			}

			// 数据合法情况
			if (CollUtil.isEmpty(errorMsg)) {
				int num = insertBamSBuildings(excel);
				if (num == 1) {
					fail++;
				}
				else {
					sucess++;
				}
			}
			else {
				// 数据不合法情况
				errorMessageList.add(new ErrorMessage(excel.getLineNum(), errorMsg));
			}
		}
		if (CollUtil.isNotEmpty(errorMessageList)) {
			return R.failed(errorMessageList);
		}
		return R.ok(sucess, "成功导入数量" + sucess + "。" + "存在" + fail + "重复");
	}

	@Override
	public List<RepairModelExcelVo> listBamSBuildings() {
		List<OmRepairModel> bamSBuildings = this.list(Wrappers.emptyWrapper());
		// 转换成execl 对象输出
		return bamSBuildings.stream().map(buildings -> {
			RepairModelExcelVo repairModelExcelVo = new RepairModelExcelVo();
			BeanUtil.copyProperties(buildings, repairModelExcelVo);
			return repairModelExcelVo;
		}).collect(Collectors.toList());
	}

	@Override
	public List<OmRepairModel> getListByModelCode(String modelCode) {
		System.out.println("打印" + omRepairModelMapper.getListByModelCode(modelCode));
		return omRepairModelMapper.getListByModelCode(modelCode);
	}

	private int insertBamSBuildings(RepairModelExcelVo excel) {
		OmRepairModel omRepairModel = new OmRepairModel();
		omRepairModel.setRepairTemplateName(excel.getRepairTemplateName());
		omRepairModel.setModelName(excel.getModelName());
		omRepairModel.setModelCode(excel.getModelCode());
		omRepairModel.setProduceFactory(excel.getProduceFactory());
		omRepairModel.setHeadInterior(excel.getHeadInterior());
		omRepairModel.setHeadPhone(excel.getHeadPhone());
		omRepairModel.setRepairUnit(excel.getRepairUnit());
		omRepairModel.setRepairPeople(excel.getRepairPeople());
		omRepairModel.setRepairPeoplePhone(excel.getRepairPeoplePhone());
		this.save(omRepairModel);
		return 0;
	}

}
